using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SchemaMonitor;
using Framework;
using SchemaMonitor.json;

partial class pages_Chapters_usercontrols_UCChapter : UserControl
{    
    #region Constants
    public const string CSS_CHECKBOX_CHANGED = "checkboxchanged";
    private static string JSCRIPT = string.Concat("this.className=(this.checked==this.defaultChecked?'':'", CSS_CHECKBOX_CHANGED, "')");
    #endregion
    
    #region Members
    private int _sectionId;
    private Guid _nodeGuid;
    private bool _isNodePage;
	private CChapterList _list;
	private CChapter _chapter;
    #endregion

    #region Behaviour
    #endregion
    
    #region Interfaces
    //Section-orientated, for Node page
    public void Display(CSection s, Guid nodeGuid, CChapter assnOrNull, int number)
    {
        if (Parent.Controls.Count % 2 == 0) row.Attributes.Add("class", "alt_row"); 
        
        _sectionId = s.SectionId;
        _nodeGuid = nodeGuid;
        _isNodePage = true;
        
        lnkTarget.Text = s.SectionName;
        //lblDateCreated.NavigateUrl = CSitemap.Books(node);

        litNum.Text = number.ToString();


		var id = Math.Abs(nodeGuid.GetHashCode()).ToString();
		if (null != assnOrNull) //Selected vs Remaining
		{
			var c = assnOrNull;
			_list = c.Section.Chapters;
			_chapter = c;

			colOrder.Visible = s.Chapters.Count > 1;
			CTextbox.SetNumber(txtOrder, c.ChapterSortOrder);

			btnDelete.ID += id;
			txt.ID += id;

			btnDelete.Visible = true;
			btnDelete.ToolTip = "Click to remove Post from this Section";
			btnAdd.Visible = false;



			foreach (var i in s.Chapters)
				if (c.ChapterSectionId != s.SectionId)
				{
					var n = i.Node;
					var lnk = new HyperLink();
					lnk.Font.Size = new FontUnit(FontSize.Smaller);
					lnk.Text = i.Section.SectionName + "::" + CUtilities.Truncate(i.ChapterName);
					lnk.NavigateUrl = CSitemap.SectionEdit(c.Section);
					lnk.Style.Add("display", "block");
					plh.Controls.Add(lnk);
				}

		}
		else
		{
			btnAdd.ID += id;

			btnAdd.Visible = true;
			btnAdd.ToolTip = "Check to Add Post to this Section";
			btnDelete.Visible = false;
			colOrder.Visible = false;
		}
	}
    //Node-orientated, for Section page
    public void Display(SchemaMonitor.CNode node, int sectionId, CChapter assnOrNull, int number)
    {
        if (Parent.Controls.Count % 2 == 0) row.Attributes.Add("class", "alt_row"); 
        
        _sectionId = sectionId;
        _nodeGuid = node.NodeGuid;
        _isNodePage = false;

		var r = node.Root;
		var post = new CSoftPost(r, CSession.User);
		
		var s = r.Name;
		var f = r.Fields;
		if (post.Message.Length > 0)
			s = post.Message;
		else if (post.HasAttachments)
			s = CUtilities.CountSummary(post.Attachments_, "photos") + " " + r.Name;
		else
			s = r.Name;

		lnkTarget.ToolTip = s;
		lnkTarget.Text = CUtilities.Truncate(lnkTarget.ToolTip, 80);


		lblDateCreated.NavigateUrl = CSitemap.ChapterEdit(node.NodeGuid); //CSitemap.Data(node.Root.Id, node.Type.TypeId);

        litNum.Text = number.ToString();

		colDateCreated.Visible = true;

		lblDateCreated.Text = CUtilities.TimespanShort(DateTime.Now.Subtract( node.NodeCreated));
		lblDateCreated.ToolTip = CUtilities.LongDateTime(node.NodeCreated);


		s = string.Empty;

		if (null != post.Attachments_ && post.Attachments_.Count > 0)
		{
			lnkPics.Text = post.Attachments_.Count.ToString();
			lnkPics.ToolTip = CUtilities.CountSummary(post.Attachments_.Count, "pic");
			//lnkPics.NavigateUrl = CSitemap.PhotoHash(EShow.ForNode, r.Guid);
		}

		if (null != post.Comments && post.Comments.Count > 0)
		{
			lblComm.Text = post.Comments.CountRecursive.ToString("n0");
			lblComm.ToolTip = CUtilities.CountSummary(post.Comments.CountRecursive, "comment");
		}


		if (null != assnOrNull && assnOrNull.ChapterPictures.Count > 0)
		{
			lnkPics.Text = assnOrNull.ChapterPictures.Count.ToString("n0");
			lnkPics.ToolTip = string.Concat(lnkPics.ToolTip.Replace("pic", "attachment") , "\r\n" , CUtilities.CountSummary(assnOrNull.ChapterPictures.Count, "pic"));
			lnkPics.NavigateUrl = CSitemap.PhotoHash(assnOrNull.ChapterId);
		}

		var id = node.Root.Id;
		if (null != assnOrNull) //Selected vs Remaining
		{
			_list = assnOrNull.Section.Chapters;
			_chapter = assnOrNull;


			lblDateCreated.NavigateUrl = CSitemap.ChapterEdit(_chapter.ChapterId);

			CDropdown.Add(ddRating, "Unrated", 0);
			CDropdown.Add(ddRating, CMetaTag.Cache.Low.TagName, CMetaTag.Cache.Low.TagId);
			CDropdown.Add(ddRating, CMetaTag.Cache.Med.TagName, CMetaTag.Cache.Med.TagId);
			CDropdown.Add(ddRating, CMetaTag.Cache.High.TagName, CMetaTag.Cache.High.TagId);
			CDropdown.SetValue(ddRating, assnOrNull.RatingTagId);

			colOrder.Visible = assnOrNull.Section.Chapters.Count > 1;
			CTextbox.SetNumber(txtOrder, _chapter.ChapterSortOrder);

			txt.ID += id;
			txt.Text = assnOrNull.ChapterName;
			if (string.IsNullOrEmpty(txt.Text))
			{
				txt.Text = CUtilities.Truncate(node.Root.Name, 50);
				assnOrNull.ChapterName = txt.Text;
				assnOrNull.Save();
			}
			txt.Visible = true;


			foreach (var i in node.Chapters)
				if (i.ChapterSectionId != sectionId)
				{
					var lnk = new HyperLink();
					lnk.Font.Size = new FontUnit(FontSize.Smaller);
					lnk.Text = i.Section.SectionName + "::" + CUtilities.Truncate( i.ChapterName);
					lnk.ToolTip =   i.Section.SectionName;
					//lnk.NavigateUrl = CSitemap.Books(node.Root.Id, node.NodeTypeId);
					lnk.NavigateUrl = CSitemap.SectionEdit(i.Section);

					lnk.Style.Add("display", "block");
					plh.Controls.Add(lnk);
				}



	btnDelete.ID += id;
			btnDelete.Visible = true;
			btnDelete.ToolTip = "Click to remove Post from this Section";
			btnAdd.Visible = false;
			//colDateCreated.Visible = true;
			//lblDateCreated.Text    = CUtilities.LongDate(    assnOrNull.XYDateCreated);
			//lblDateCreated.ToolTip = CUtilities.LongDateTime(assnOrNull.XYDateCreated);
		}
		else
		{
			lnkTarget.Text = CUtilities.Truncate(lnkTarget.ToolTip, 120);
			btnAdd.ID += id;
			btnAdd.Visible = true;
			btnAdd.ToolTip = "Check to Add Post to this Section";
			btnDelete.Visible = false;
			colOrder.Visible = false;
			colRating.Visible = false;
		}
	}
	#endregion

	#region Events

	protected void btnMoveUp_Click(object sender, System.Web.UI.ImageClickEventArgs e)
	{
		new CChapterList(_list).MoveUp(_chapter);
		Refresh();
	}
	protected void btnMoveDn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
	{
		new CChapterList(_list).MoveDown(_chapter);
		Refresh();
	}
	protected void btnAdd_Click(object sender, ImageClickEventArgs e)
	{
		var c = new CChapter();
		c.ChapterSectionId = _sectionId;
		c.ChapterNodeGuid = _nodeGuid;
		c.Save();
		Refresh();
	}
	protected void btnDelete_Click(object sender, ImageClickEventArgs e)
	{
		CChapter.Cache.GetBySectionId(_sectionId).GetByNodeGuid(_nodeGuid)[0].Delete();
		Refresh();
	}

    private void Refresh()
    {
        Response.Redirect(Request.RawUrl);
    }
	#endregion


	protected void txtOrder_TextChanged(object sender, EventArgs e)
	{
		var i = CTextbox.GetInteger(txtOrder);
		if (i == int.MinValue)
		{
			_chapter.ChapterSortOrder = i;
			_chapter.Save();
			_list.Sort();
			return;
		}

		if (i < 0)
			i = 0;
		if (i >= _list.Count)
			i = _list.Count-1;

		var temp = new CChapterList(_list);
		temp.Remove(_chapter);
		temp.Insert(i, _chapter);
		temp.ResetSortOrder();
		temp.SaveAll();
		_list.Sort();
		Refresh();
	}
	protected void txt_TextChanged(object sender, EventArgs e)
	{
		_chapter.ChapterName = txt.Text;
		_chapter.Save();
		Refresh();
	}

	protected void ddRating_SelectedIndexChanged(object sender, EventArgs e)
	{
		_chapter.RatingTagId = CDropdown.GetInt(ddRating);
		_chapter.Save();
		Refresh();
	}
}
